<?php
/* @var $this ProjectController */
/* @var $model Exam */
/* @var $form CActiveForm */
?>
<script type="text/javascript">
$(function() {
	$( "#exam-form" ).submit(function( event ) {
		if (($('#cname3').val()==$('#cname1').val()&&$('#cname3').val()!='')||($('#cname3').val()==$('#cname2').val()&&$('#cname3').val()!='')||($('#cname1').val()==$('#cname2').val()&&$('#cname2').val()!='')){
			$('#dialogError2 .errorForm2').html('<br><br><div align="center">บันทึกข้อมูลผู้ทรงคุณวุฒิซ้ำ</div>');
			$('#dialogError2').dialog("open");
		  event.preventDefault();
		}else{
			return;
			}
		});
	$('#cname3').blur(function(){
		if (($('#cname3').val()==$('#cname1').val()&&$('#cname3').val()!='')||($('#cname3').val()==$('#cname2').val()&&$('#cname2').val()!='')) {
			$('#dialogError2 .errorForm2').html('<br><br><div align="center">บันทึกข้อมูลผู้ทรงคุณวุฒิซ้ำ</div>');
			$('#dialogError2').dialog("open");
			//$('#cname3').focus();
			}
	});
	$('#cname2').blur(function(){
		
		if (($('#cname2').val()==$('#cname1').val()&&$('#cname2').val()!='')||($('#cname3').val()==$('#cname2').val()&&$('#cname2').val()!='')) {
			
			$('#dialogError2 .errorForm2').html('<br><br><div align="center">บันทึกข้อมูลผู้ทรงคุณวุฒิซ้ำ</div>');
			
			$('#dialogError2').dialog("open");
			//alert($('#dialogError2 .errorForm2').html());
			//$('#cname3').focus();
			}
	});
	$('#cname1').blur(function(){
		if (($('#cname3').val()==$('#cname1').val()&&$('#cname1').val()!='')||($('#cname1').val()==$('#cname2').val()&&$('#cname1').val()!='')) {
			$('#dialogError2 .errorForm2').html('<br><br><div align="center">บันทึกข้อมูลผู้ทรงคุณวุฒิซ้ำ</div>');
			$('#dialogError2').dialog("open");
			//$('#cname3').focus();
			}
	});
	$('#Exam_examRoom').blur(
			function (){
				this.value = this.value.toUpperCase();
			}
			);
	$('[name="commitTypeID[0]"]').click(function(){
		
		if ($('#commit1 [name="commitTypeID[0]"]:checked').val()==1)
		{
			$("#commit1 #teacher").show();
			$("#commit1 #other").hide();
		}else{
			$("#commit1 #teacher").hide();
			$("#commit1 #other").show();
		}
	});
$('[name="commitTypeID[1]"]').click(function(){
		
		if ($('#commit2 [name="commitTypeID[1]"]:checked').val()==1)
		{
			$("#commit2 #teacher").show();
			$("#commit2 #other").hide();
		}else{
			$("#commit2 #teacher").hide();
			$("#commit2 #other").show();
		}
	});
$('[name="commitTypeID[2]"]').click(function(){
	
	if ($('#commit3 [name="commitTypeID[2]"]:checked').val()==1)
	{
		$("#commit3 #teacher").show();
		$("#commit3 #other").hide();
	}else{
		$("#commit3 #teacher").hide();
		$("#commit3 #other").show();
	}
});
<?php if($commit1->commitTypeID==2){?>
$("#commit1 #teacher").hide();
$('#commit1 input[name="commitTypeID[0]"][value=2]').prop("checked",true);
	<?php }else{?>
	$("#commit1 #other").hide();
	
	<?php }?>
	<?php if($commit2->commitTypeID==2){?>
	
	$("#commit2 #teacher").hide();
	$('#commit2 input[name="commitTypeID[1]"][value=2]').prop("checked",true);
	<?php }else{?>
	$("#commit2 #other").hide();
	<?php }?>
	<?php if($commit3->commitTypeID==2){?>
	$("#commit3 #teacher").hide();
	$('#commit3 input[name="commitTypeID[2]"][value=2]').prop("checked",true);
	<?php }else{?>
	$("#commit3 #other").hide();
	
	<?php }?>

});
</script>
<div class="form">

<?php

$form = $this->beginWidget ( 'CActiveForm', array (
		'id' => 'exam-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableAjaxValidation' => true 
) );
?>

<?php $model->examDate=DateTimeArray::convertMysqlToThai($model->examDate)?>

	<?php echo $form->errorSummary($model); ?>


	<div class="row">
		<?php echo $form->labelEx($model,'examDate'); ?>
		<?php
		$this->widget ( 'zii.widgets.jui.CJuiDatePicker', array (
				'model' => $model,
				'attribute' => 'examDate',
				'language' => 'th',
				// additional javascript options for the date picker plugin
				'options' => array (
						// 'mode'=>'focus',
						// 'dateFormat'=>'d MM, yy',
						// 'showOn'=>'button', // 'focus', 'button', 'both'
						'dateFormat' => 'dd/mm/yy',
						// 'value'=>strftime('%d/%m/%Y', strtotime($model->docDate)),
						'showAnim' => 'fold',
						'changeMonth' => true,
						'changeYear' => true,
						'beforeShow' => 'js:function(){
	if($(this).val()!=""){
		var arrayDate=$(this).val().split("/");
		arrayDate[2]=parseInt(arrayDate[2])-543;
		$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
	}
	setTimeout(function(){
		$.each($(".ui-datepicker-year option"),function(j,k){
			var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
			$(".ui-datepicker-year option").eq(j).text(textYear);
		});
	},50);

}',
						'onChangeMonthYear' => 'js:function(){
	setTimeout(function(){
		$.each($(".ui-datepicker-year option"),function(j,k){
			var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
			$(".ui-datepicker-year option").eq(j).text(textYear);
		});
	},50);
}',
						'onClose' => 'js:function(){
	if($(this).val()!="" && $(this).val()==dateBefore){
		var arrayDate=dateBefore.split("/");
		arrayDate[2]=parseInt(arrayDate[2])+543;
		$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
	}
}',
						'onSelect' => 'js:function(dateText, inst){
	dateBefore=$(this).val();
	var arrayDate=dateText.split("/");
	arrayDate[2]=parseInt(arrayDate[2])+543;
	$(this).val(arrayDate[0]+"/"+arrayDate[1]+"/"+arrayDate[2]);
}' 
				)
				,
				'htmlOptions' => array (
						'style' => 'height:20px;' 
				)
				 
		) );
		?>
		
		<?php echo $form->error($model,'examDate'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'examTime'); ?>
		<?php echo $form->timeField($model,'examTime'); ?>
		<?php echo $form->error($model,'examTime'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'examRoom'); ?>
		<?php echo $form->textField($model,'examRoom'); ?>
		<?php echo $form->error($model,'examRoom'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'floor'); ?>
		<?php echo $form->textField($model,'floor'); ?>
		<?php echo $form->error($model,'floor'); ?>
	</div>
	<div id="commit1">
		<div class="row">
		<?php echo $form->labelEx($commit1,'commitTypeID'); ?>
		<?php echo CHtml::radioButton('commitTypeID[0]',true,array("value"=>1,'uncheckValue'=>null)); ?> อาจารย์  
		<?php echo CHtml::radioButton('commitTypeID[0]',false,array("value"=>2,'uncheckValue'=>null)); ?> บุคคลภายนอก
		<?php echo $form->error($commit1,'commitTypeID'); ?>
	</div>
		<div id="teacher">
			<div class="row">
		<?php echo $form->labelEx($regTeacher1,'name'); ?>
		<?php echo CHtml::textField('name1',$regTeacher1->name,array('size'=>20,'maxlength'=>40,'readonly'=>'readonly')); ?>
		<?php echo CHtml::hiddenField('user_id1',$regTeacher1->user_id);?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchMinorTeacher1',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
				addTeacher(1);
				//$("#dialogTeacher").dialog("destroy");
 				$("#dialogTeacher1").dialog("open"); return false;}'),
		));
		?> 
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnClearMinorTeacher1',
				'caption'=>'ยกเลิก',
				'onclick'=>new CJavaScriptExpression('function(){
				//$("#dialogTeacher").dialog("destroy");
 				$("#minorAdvisorID").val("");
				$("#minorAdvisor").val("");
				 return false;}'),
		));
		?>
		<?php echo $form->error($regTeacher1,'name'); ?>
	</div>
		</div>
		<div id="other">
			<div class="row">
		<?php echo $form->labelEx($commit1,'title'); ?>
		<?php echo CHtml::textField('title1',$commit1->title,array('size'=>8,'maxlength'=>45)); ?>
		<?php echo $form->error($commit1,'title'); ?>
	</div>

			<div class="row">
		<?php echo $form->labelEx($commit1,'name'); ?>
		<?php echo CHtml::textField('cname1',$commit1->name,array('size'=>60,'maxlength'=>150)); ?>
		<?php echo $form->error($commit1,'name'); ?>
	</div>
		</div>
	</div>
	<div id="commit2">
		<div class="row">
		<?php echo $form->labelEx($commit2,'commitTypeID'); ?>
		<?php echo CHtml::radioButton('commitTypeID[1]',true,array("value"=>1,'uncheckValue'=>null)); ?> อาจารย์  
		<?php echo CHtml::radioButton('commitTypeID[1]',false,array("value"=>2,'uncheckValue'=>null)); ?> บุคคลภายนอก
		<?php echo $form->error($commit2,'commitTypeID'); ?>
	</div>
		<div id="teacher">
			<div class="row">
		<?php echo $form->labelEx($regTeacher2,'name'); ?>
		<?php echo CHtml::textField('name2',$regTeacher2->name,array('size'=>20,'maxlength'=>40,'readonly'=>'readonly')); ?>
		<?php echo CHtml::hiddenField('user_id2',$regTeacher2->user_id);?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchMinorTeacher2',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
				addTeacher(2);
				//$("#dialogTeacher").dialog("destroy");
 				$("#dialogTeacher2").dialog("open"); return false;}'),
		));
		?> 
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnClearMinorTeacher2',
				'caption'=>'ยกเลิก',
				'onclick'=>new CJavaScriptExpression('function(){
				//$("#dialogTeacher").dialog("destroy");
 				$("#minorAdvisorID").val("");
				$("#minorAdvisor").val("");
				 return false;}'),
		));
		?>
		<?php echo $form->error($regTeacher2,'name'); ?>
	</div>
		</div>
		<div id="other">
			<div class="row">
		<?php echo $form->labelEx($commit2,'title'); ?>
		<?php echo CHtml::textField('title2',$commit2->title,array('size'=>8,'maxlength'=>45)); ?>
		<?php echo $form->error($commit2,'title'); ?>
	</div>

			<div class="row">
		<?php echo $form->labelEx($commit2,'name'); ?>
		<?php echo CHtml::textField('cname2',$commit2->name,array('size'=>60,'maxlength'=>150)); ?>
		<?php echo $form->error($commit2,'name'); ?>
	</div>
		</div>
	</div>
	<div id="commit3">
		<div class="row">
		<?php echo $form->labelEx($commit3,'commitTypeID'); ?>
		<?php echo CHtml::radioButton('commitTypeID[2]',true,array("value"=>1,'uncheckValue'=>null)); ?> อาจารย์  
		<?php echo CHtml::radioButton('commitTypeID[2]',false,array("value"=>2,'uncheckValue'=>null)); ?> บุคคลภายนอก
		<?php echo $form->error($commit3,'commitTypeID'); ?>
	</div>
		<div id="teacher">
			<div class="row">
		<?php echo $form->labelEx($regTeacher3,'name'); ?>
		<?php echo CHtml::textField('name3',$regTeacher3->name,array('size'=>20,'maxlength'=>40,'readonly'=>'readonly')); ?>
		<?php echo CHtml::hiddenField('user_id3',$regTeacher3->user_id);?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchMinorTeacher3',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
				addTeacher(3);
				//$("#dialogTeacher").dialog("destroy");
 				$("#dialogTeacher3").dialog("open"); return false;}'),
		));
		?> 
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnClearMinorTeacher3',
				'caption'=>'ยกเลิก',
				'onclick'=>new CJavaScriptExpression('function(){
				//$("#dialogTeacher").dialog("destroy");
 				$("#minorAdvisorID").val("");
				$("#minorAdvisor").val("");
				 return false;}'),
		));
		?>
		<?php echo $form->error($regTeacher3,'name'); ?>
	</div>
		</div>
		<div id="other">
			<div class="row">
		<?php echo $form->labelEx($commit3,'title'); ?>
		<?php echo CHtml::textField('title3',$commit3->title,array('size'=>8,'maxlength'=>45)); ?>
		<?php echo $form->error($commit3,'title'); ?>
	</div>

			<div class="row">
		<?php echo $form->labelEx($commit3,'name'); ?>
		<?php echo CHtml::textField('cname3',$commit3->name,array('size'=>60,'maxlength'=>150)); ?>
		<?php echo $form->error($commit3,'name'); ?>
	</div>
		</div>
	</div>
	<div class="row buttons">
		<?php 
	$this->widget('zii.widgets.jui.CJuiButton',array(
			'buttonType'=>'submit',
			'name'=>'btnSubmit',
			'value'=>'1',
			'caption'=>$model->isNewRecord ? 'บันทึกข้อมูล' : 'แก้ไขข้อมูล',
			'htmlOptions'=>array('class'=>'ui-button-primary'),
	)
	);
	?>
	</div>

<?php $this->endWidget(); ?>

</div>
<!-- form -->


<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogTeacher1',
    'options'=>array(
        'title'=>'ค้นหาอาจารย์',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
		'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogTeacher2',
    'options'=>array(
        'title'=>'ค้นหาอาจารย์',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogTeacher3',
    'options'=>array(
        'title'=>'ค้นหาอาจารย์',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>

<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogError',
    'options'=>array(
        'title'=>'เกิดข้อผิดลาพ',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>350,
        'height'=>270,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="errorForm"></div>
 
<?php $this->endWidget();?>
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogError2',
    'options'=>array(
        'title'=>'เกิดข้อผิดลาพ',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>350,
        'height'=>270,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="errorForm2"></div>
 
<?php $this->endWidget();?>
<script type="text/javascript">
// here is the magic
 function addTeacher(id)
 {
      $.ajax({'url':'<?=Yii::app()->createUrl('regTeacher/listTeacher')?>/'+id,'type':'get','success':function(data)
            {
          			if (id==1){
                    $('#dialogTeacher1 div.divForForm').html(data);
          			}else if(id==2){
          				$('#dialogTeacher2 div.divForForm').html(data);
          			}else if(id==3){
          				$('#dialogTeacher3 div.divForForm').html(data);
              			}
          			

            } ,'cache':false});
     return false; 
  
 }
  function getTeacher(id,type)
  {	 
 	 $(function () {
 		
 		 $.getJSON('<?=Yii::app()->createUrl('regTeacher/getTeacher')?>/?id='+id,function(data)
 	            {
	            if (type==1){
	            	if (data['teacherID']==$('#user_id2').val()||data['teacherID']==$('#user_id3').val()) {
	            		$('.errorForm').html('<br><br><div align="center">เลือกอาจารย์ซ้ำ</div>');
		            	$('#dialogError').dialog("open");
		            }else{
 			 			$('#user_id1').val(data['teacherID']);
 			 			$('#name1').val(data['name']);
 	                    //$('#majorAdvisor').val(data['name']);
		            }

	            }else if (type==2){
	            	if (data['teacherID']==$('#user_id1').val()||data['teacherID']==$('#user_id3').val()) {
	            		$('.errorForm').html('<br><br><div align="center">เลือกอาจารย์ซ้ำ</div>');
		            	$('#dialogError').dialog("open");
		            }else{
	            	$('#user_id2').val(data['teacherID']);
	            	$('#name2').val(data['name']);
		            }

	            } else if (type==3){
	            	if (data['teacherID']==$('#user_id2').val()||data['teacherID']==$('#user_id1').val()) {
	            		$('.errorForm').html('<br><br><div align="center">เลือกอาจารย์ซ้ำ</div>');
		            	$('#dialogError').dialog("open");
		            }else{
	                    	$('#user_id3').val(data['teacherID']);
	                    	$('#name3').val(data['name']);
		            }

			            }
 	                    
 	            } );
 	 } );
 		 
  }
</script>